#-*- encoding:utf-8 -*-
import numpy as np
import sys;
import matplotlib;
import matplotlib.pyplot as plt
reload(sys)
sys.setdefaultencoding('utf8')
matplotlib.rcParams['axes.unicode_minus'] = False
slice_size=31
trace_num=200
num_point=200
add_point=31
side_add_point=(add_point-1)/2
true_set_num=150
false_set_num=200


def get_true_data():
    f=open('data/true_set.txt')
    text=f.read()
    f.close()
    true_set=[]
    text_list=text.split('\r\n')
    len_t=len(text_list)
    for i in range(len_t-1):
        line_data_list=[]
        line_list=text_list[i].split()
        for j in range(len(line_list)):
            line_data_list.append(float(line_list[j]))
        true_set.append(line_data_list)
    true_arr= np.array(true_set, dtype = float)
    return true_arr

def get_false_data():
    f=open('data/false_set.txt')
    text = f.read()
    f.close()
    false_set = []
    text_list = text.split('\r\n')
    for i in range(slice_size):
        line_data_list = []
        line_list = text_list[i].split()
        for j in range(len(line_list)):
            line_data_list.append(float(line_list[j]))
        false_set.append(line_data_list)
    false_arr = np.array(false_set, dtype=float)
    false_arr=false_arr.T
    return false_arr

def get_ture_label():
    f = open('data/true_label.txt')
    text = f.read()
    f.close()
    true_set = []
    text_list = text.split('\r\n')
    len_t = len(text_list)
    for i in range(len_t-1):
       true_set.append(float(text_list[i]))
    true_label = np.array(true_set, dtype=float)
    true_label=true_label.reshape((600,1))
    return true_label

def get_false_label():
    label_false=np.zeros((false_set_num,1),dtype=float)
    label_false[:]=0
    return label_false

def get_set_and_label():
    set1=get_true_data()
    label1=get_ture_label()
    train_x=set1
    label_y=label1
    return train_x,label_y

def get_single_noise0__data():
    f = open('data/data_of_single_trace.txt', 'r')
    text = f.read()
    f.close()
    test_list = text.split('\r\n')
    data_set = []
    for i in range(trace_num):
        data_set.append(float(test_list[i]))
    data_single=np.array(data_set)
    arr_request = np.zeros((num_point, slice_size), dtype=float)
    add_data1 = np.zeros(side_add_point, dtype=float)
    add_data2 = np.zeros(side_add_point, dtype=float)
    add_data1[:] = data_single[0:side_add_point]
    add_data2[:]=data_single[-side_add_point:]
    data_new = np.append(add_data1,data_single)
    data_new = np.append(data_new,add_data2)
    data_new = np.abs(data_new)
    max = np.max(data_new)
    min = np.min(data_new)
    data_new = (data_new - min) / (max - min)
    for i in range(num_point):
        arr_request[i,:]=data_new[i:i+add_point]
    return arr_request,data_single

def get_single_noise10__data():
    f = open('data/noise10_single_data.txt', 'r')
    text = f.read()
    f.close()
    test_list = text.split('\r\n')
    data_set = []
    for i in range(trace_num):
        data_set.append(float(test_list[i]))
    data_single = np.array(data_set)
    arr_request = np.zeros((num_point, slice_size), dtype=float)
    add_data1 = np.zeros(side_add_point, dtype=float)
    add_data2 = np.zeros(side_add_point, dtype=float)
    add_data1[:] = data_single[0:side_add_point]
    add_data2[:] = data_single[-side_add_point:]
    data_new = np.append(add_data1, data_single)
    data_new = np.append(data_new, add_data2)
    data_new = np.abs(data_new)
    max = np.max(data_new)
    min = np.min(data_new)
    data_new = (data_new - min) / (max - min)
    for i in range(num_point):
        arr_request[i, :] = data_new[i:i + add_point]
    return arr_request, data_single

def get_single_noise20__data():
    f = open('data/noise20_single_data.txt', 'r')
    text = f.read()
    f.close()
    test_list = text.split('\r\n')
    data_set = []
    for i in range(trace_num):
        data_set.append(float(test_list[i]))
    data_single = np.array(data_set)
    arr_request = np.zeros((num_point, slice_size), dtype=float)
    add_data1 = np.zeros(side_add_point, dtype=float)
    add_data2 = np.zeros(side_add_point, dtype=float)
    add_data1[:] = data_single[0:side_add_point]
    add_data2[:] = data_single[-side_add_point:]
    data_new = np.append(add_data1, data_single)
    data_new = np.append(data_new, add_data2)
    data_new = np.abs(data_new)
    max = np.max(data_new)
    min = np.min(data_new)
    data_new = (data_new - min) / (max - min)
    for i in range(num_point):
        arr_request[i, :] = data_new[i:i + add_point]
    return arr_request, data_single

def get_single_noise30__data():
    f = open('data/noise30_single_data.txt', 'r')
    text = f.read()
    f.close()
    test_list = text.split('\r\n')
    data_set = []
    for i in range(trace_num):
        data_set.append(float(test_list[i]))
    data_single = np.array(data_set)
    arr_request = np.zeros((num_point, slice_size), dtype=float)
    add_data1 = np.zeros(side_add_point, dtype=float)
    add_data2 = np.zeros(side_add_point, dtype=float)
    add_data1[:] = data_single[0:side_add_point]
    add_data2[:] = data_single[-side_add_point:]
    data_new = np.append(add_data1, data_single)
    data_new = np.append(data_new, add_data2)
    data_new = np.abs(data_new)
    max = np.max(data_new)
    min = np.min(data_new)
    data_new = (data_new - min) / (max - min)
    for i in range(num_point):
        arr_request[i, :] = data_new[i:i + add_point]
    return arr_request, data_single
if __name__=='__main__':
    x,y=get_set_and_label()
    print x.shape
    print y.shape